package com.ruoyi.app.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.app.domain.ProductCategory;
import com.ruoyi.app.domain.ShopInfo;
import com.ruoyi.app.domain.vo.ProductInfoVo;
import com.ruoyi.app.enums.ShopInfoTypeEnum;
import com.ruoyi.app.service.IProductCategoryService;
import com.ruoyi.app.service.IShopInfoService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MybatisUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.app.mapper.ProductInfoMapper;
import com.ruoyi.app.domain.ProductInfo;
import com.ruoyi.app.service.IProductInfoService;
import org.springframework.util.CollectionUtils;

/**
 * 商品Service业务层处理
 * 
 * @author zkq
 * @date 2024-07-28
 */
@Service
public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, ProductInfo> implements IProductInfoService
{
    @Autowired
    private ProductInfoMapper productInfoMapper;

    @Autowired
    private IProductCategoryService productCategoryService;

    @Autowired
    private IShopInfoService shopInfoService;

    /**
     * 查询商品
     * 
     * @param id 商品主键
     * @return 商品
     */
    @Override
    public ProductInfo selectProductInfoById(String id)
    {
        return baseMapper.selectById(id);
    }

    /**
     * 查询商品列表
     * 
     * @param productInfo 商品
     * @return 商品
     */
    @Override
    public List<ProductInfo> selectProductInfoList(ProductInfo productInfo)
    {
        LambdaQueryWrapper<ProductInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Map<String, Object> params = productInfo.getParams();
        Object beginTime = params.get("beginTime");
        Object endTime = params.get("endTime");
        lambdaQueryWrapper.eq(ProductInfo::getShopId, productInfo.getShopId());
        ShopInfo shopInfo = shopInfoService.selectShopInfoById(productInfo.getShopId());
        // 获取蛋店铺类型
        if(shopInfo != null && ShopInfoTypeEnum.NOT_LINKAGE.getType().equals(shopInfo.getType())){
            lambdaQueryWrapper.eq(ProductInfo::getType, productInfo.getType());
        }
        lambdaQueryWrapper.like(StringUtils.isNotBlank(productInfo.getName()), ProductInfo::getName, productInfo.getName());
        lambdaQueryWrapper.between((beginTime!= null && endTime!= null), ProductInfo::getCreateTime, beginTime, endTime);
        return baseMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 根据分类查询商品
     * @param categoryId
     * @return
     */
    @Override
    public List<ProductInfo> selecProductCategoryById(String categoryId) {
        if(StringUtils.isEmpty(categoryId)){
           return null;
        }
        LambdaQueryWrapper<ProductInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>(ProductInfo.class);
        lambdaQueryWrapper.eq(ProductInfo::getCategoryId, categoryId);
        return baseMapper.selectList(lambdaQueryWrapper);
    }


    /**
     * 添加商品
     * @param productInfo
     * @return
     */
    @Override
    public String addOrUpdate(ProductInfo productInfo) {
        if(!verification(productInfo)){
            throw new ServiceException("商品名称已存在");
        }
        if(StringUtils.isNotBlank(productInfo.getCategoryId())){
            ProductCategory productCategory = productCategoryService.selectProductCategoryById(productInfo.getCategoryId());
            if(productCategory != null){
                productInfo.setCategoryTxt(productCategory.getCategoryName());
            }
        }
        if(StringUtils.isBlank(productInfo.getId())){
//            productInfo.setCurrentNumber(productInfo.getTotalNumber());
            productInfo.setTotalNumber(productInfo.getCurrentNumber());
            save(productInfo);
        } else {
            updateById(productInfo);
        }
        return productInfo.getId();
    }


    /**
     * 添加商品数量
     * @param productInfo
     * @return
     */
    @Override
    public String addProductNumber(ProductInfo productInfo) {
        if(StringUtils.isBlank(productInfo.getId()) || productInfo.getTotalNumber() == null){
            throw new ServiceException("增加数据不能为空");
        }
        ProductInfo saveProductInfo = new ProductInfo();
        ProductInfo productInfo1 = selectProductInfoById(productInfo.getId());
        if(productInfo1 == null){
            throw new ServiceException("增加数据不能为空");
        }
        saveProductInfo.setTotalNumber((productInfo1.getTotalNumber() == null ? 0 : productInfo1.getTotalNumber())+productInfo.getTotalNumber());
        saveProductInfo.setCurrentNumber((productInfo1.getCurrentNumber() == null ? 0 :productInfo1.getCurrentNumber())+productInfo.getTotalNumber());
        saveProductInfo.setId(productInfo.getId());
        updateById(saveProductInfo);
        return saveProductInfo.getId();
    }

    /**
     * 销售统计
     * @param productInfoVo
     * @return
     */
    @Override
    public int salesStatistics(ProductInfoVo productInfoVo) {
        return 0;
    }

    /**
     * 验证商品
     * @param productInfo
     * @return
     */
    @Override
    public Boolean verification(ProductInfo productInfo) {
        if(productInfo == null || StringUtils.isEmpty(productInfo.getShopId()) || StringUtils.isEmpty(productInfo.getName())){
            return false;
        }
        LambdaQueryWrapper<ProductInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ProductInfo::getShopId,productInfo.getShopId());
        lambdaQueryWrapper.eq(ProductInfo::getName,productInfo.getName());
        lambdaQueryWrapper.eq(ProductInfo::getDelFlag, Constants.NOT_DEL);
        lambdaQueryWrapper.ne(StringUtils.isNotBlank(productInfo.getId()), ProductInfo::getId, productInfo.getId());
        Integer i = baseMapper.selectCount(lambdaQueryWrapper);
        return i <= 0;
    }

    /**
     * 验证商品
     * @param shopId
     * @param productNames
     * @return
     */
    @Override
    public Boolean verification(String shopId, Set<String> productNames) {
        if(StringUtils.isEmpty(shopId) || CollectionUtils.isEmpty(productNames)){
            return false;
        }
        LambdaQueryWrapper<ProductInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ProductInfo::getShopId, shopId);
        lambdaQueryWrapper.in(ProductInfo::getName, productNames);
        lambdaQueryWrapper.eq(ProductInfo::getDelFlag, Constants.NOT_DEL);
        Integer i = baseMapper.selectCount(lambdaQueryWrapper);
        return i <= 0;
    }

}
